package com.yunhe.tianhe.rentcars.controller.permission;

import com.yunhe.tianhe.commons.dto.Pageable;
import com.yunhe.tianhe.rentcars.dto.JSONResponse;
import com.yunhe.tianhe.rentcars.entity.permission.Resource;
import com.yunhe.tianhe.rentcars.service.permission.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lanyangyun
 * @version V1.0
 * @Description: //TODO
 * @date 2017/10/19 0019 上午 11:47
 */
@Controller
@RequestMapping("/resource")

public class ResourceController {

    @Autowired
    private ResourceService ps;

    @RequestMapping
    public String list(HttpServletRequest req ){
        return "/permission/resource/list";
    }

    @RequestMapping("/add")
    public String add(){
        return "/permission/resource/add";
    }

    @RequestMapping("/edit")
    public String edit(HttpServletRequest req, Long id){
        Resource resource = ps.find(id);
        Integer type = resource.getPermisType();
        List<Resource> parentList = null;
        if (type!=null&&type!=1&&type!=9){
            parentList = ps.findByType(type==2?1:2);
        }
        req.setAttribute("resource",resource);
        req.setAttribute("parentList",parentList);
        return "/permission/resource/edit";
    }

    @RequestMapping("/save")
    public String save(Resource resource){
        ps.save(resource);
        return "redirect:/resource";
    }

    @RequestMapping("/update")
    public String update(Resource resource){
        ps.update(resource);
        return "redirect:/resource";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONResponse delete(String  ids){
        ps.deleteBatch(ids);
        return JSONResponse.success("删除成功！");
    }

    @RequestMapping("/type/list")
    @ResponseBody
    public JSONResponse findByType(Integer type){
        List<Resource> resourceList = ps.findByType(type);
        return  JSONResponse.success("返回结果",resourceList);
    }

    @RequestMapping("/pageData")
    @ResponseBody
    public Pageable<Resource> findPage(Integer offset, Integer limit){
        Pageable<Resource> pageable = ps.findPage(offset, limit);
        return pageable;
    }

    @RequestMapping("/subList")
    @ResponseBody
    public List<Resource> subList(long parentId){
        List<Resource> resourceList = ps.findByParentId(parentId);
        return resourceList;
    }
}
